package org.opencv.ivvi.custom;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * Created by duanxianping on 2017/6/7.
 */

public class BitmapHelper {

    /**
     * 创建不同方位bitmap
     *
     * @param source
     * @param cropType
     * @return
     */
    public static Bitmap createBitmap(Bitmap source, int cropType) {
        Bitmap bitmap = null;
        switch (cropType) {
            case CropType.LEFT:
                bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth() / 2, source.getHeight());
                break;
            case CropType.RIGHT:
                bitmap = Bitmap.createBitmap(source, source.getWidth() / 2, 0, source.getWidth() / 2, source.getHeight());
                break;
            case CropType.TOP:
                bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight() / 2);
                break;
            case CropType.BOTTOM:
                bitmap = Bitmap.createBitmap(source, 0, source.getHeight() / 2, source.getWidth(), source.getHeight() / 2);
                break;
            default:
                break;
        }
        return bitmap;
    }

    /**
     * obtain the image's width and height
     *
     * @param imagePath the path of image
     */
    private static int[] getImageSize(String imagePath) {
        int[] res = new int[2];

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inSampleSize = 1;
        BitmapFactory.decodeFile(imagePath, options);

        res[0] = options.outWidth;
        res[1] = options.outHeight;

        return res;
    }

    /**
     * obtain the thumbnail that specify the size
     *
     * @param imagePath the target image path
     * @param width     the width of thumbnail
     * @param height    the height of thumbnail
     * @return {@link Bitmap}
     */
    public static Bitmap compress(String imagePath, int width, int height) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath, options);

        int outH = options.outHeight;
        int outW = options.outWidth;
        int inSampleSize = 1;

        if (outH > height || outW > width) {
            int halfH = outH / 2;
            int halfW = outW / 2;

            while ((halfH / inSampleSize) > height && (halfW / inSampleSize) > width) {
                inSampleSize *= 2;
            }
        }

        options.inSampleSize = inSampleSize;

        options.inJustDecodeBounds = false;

        int heightRatio = (int) Math.ceil(options.outHeight / (float) height);
        int widthRatio = (int) Math.ceil(options.outWidth / (float) width);

        if (heightRatio > 1 || widthRatio > 1) {
            if (heightRatio > widthRatio) {
                options.inSampleSize = heightRatio;
            } else {
                options.inSampleSize = widthRatio;
            }
        }
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(imagePath, options);
    }

    public static Bitmap compress(String imagePath) {
        int ratio = 1;
        Bitmap bitmap = compress(imagePath, Constants.WIDTH * ratio, Constants.HEIGHT * ratio);
        return bitmap;
    }

}
